package com.sixth.service.impl;

import com.sixth.bean.MarketCategory;
import com.sixth.bean.MarketCategoryExample;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.*;
import com.sixth.mapper.MarketCategoryMapper;
import com.sixth.service.WXCatalogService;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXCatalogServiceImpl implements WXCatalogService {


    @Autowired
    MarketCategoryMapper  marketCategoryMapper;
   //实现类方法


    @SneakyThrows
    @Override
    public WXCatalogIndexVO wxIndex() {
        WXCatalogIndexVO catalogIndexVO = new WXCatalogIndexVO();

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(null);
        ArrayList<CategoryListDTO> categoryListDTOS = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            CategoryListDTO categoryListDTO = new CategoryListDTO();
            BeanUtils.copyProperties(categoryListDTO,marketCategory);

            categoryListDTOS.add(categoryListDTO);

        }
        //查询一级列表的类目
        catalogIndexVO.setCategoryList(categoryListDTOS);
//------------------------------------------------------------------------------------------

        MarketCategory marketCategory1 = marketCategoryMapper.selectByPrimaryKey(categoryListDTOS.get(0).getId());

        CurrentCategoryDTO currentCategoryDTO = new CurrentCategoryDTO();

        BeanUtils.copyProperties(currentCategoryDTO,marketCategory1);

        //查询当前类目，第一个类目，一级类目
        catalogIndexVO.setCurrentCategory(currentCategoryDTO);
//-----------------------------------------------------------------------------

        List<MarketCategory> marketCategories2 = marketCategoryMapper.selectByExample(null);

        ArrayList<CurrentSubCategoryDTO> currentSubCategoryDTOS = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories2) {
            CurrentSubCategoryDTO currentSubCategoryDTO = new CurrentSubCategoryDTO();

            BeanUtils.copyProperties(currentSubCategoryDTO,marketCategory);

            currentSubCategoryDTOS.add(currentSubCategoryDTO);

        }
        //当前类目的子类目，第一个类目的二级类目
        catalogIndexVO.setCurrentSubCategory(currentSubCategoryDTOS);
//------------------------------------------------------------------------------
        return catalogIndexVO;
    }

    @SneakyThrows
    @Override
    public WXCatalogCurrentVO wxCurrent(Integer id) {
        WXCatalogCurrentVO catalogCurrentVO = new WXCatalogCurrentVO();

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(null);


        CurrentCategoryDTO currentCategoryDTO = new CurrentCategoryDTO();

        BeanUtils.copyProperties(currentCategoryDTO,marketCategories);

        catalogCurrentVO.setCurrentCategory(currentCategoryDTO);

//-----------------------------------------------------------------------------

        List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(null);

        List<CurrentSubCategoryDTO> currentSubCategoryDTOS = new ArrayList<>();

        for (MarketCategory category : marketCategories1 ) {

            CurrentSubCategoryDTO currentSubCategoryDTO = new CurrentSubCategoryDTO();

            BeanUtils.copyProperties(currentSubCategoryDTO,category);

            currentSubCategoryDTOS.add(currentSubCategoryDTO);

        }

        catalogCurrentVO.setCurrentSubCategory(currentSubCategoryDTOS);


        return catalogCurrentVO;
    }

}
